/*
 * @Author: 晴鸢. 3013433857@qq.com
 * @Date: 2025-11-05 08:12:05
 * @LastEditors: 晴鸢. 3013433857@qq.com
 * @LastEditTime: 2025-11-05 19:32:53
 * @FilePath: /Learn/Webself/ClientCal.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<time.h>
#include<assert.h>
#include<unistd.h>
#include"socket.hpp"
#include"protohol.hpp"
#include"Log.hpp"
void Debugprint(const std::string &str)
{
    std::cout<<str<<" port ip"<<std::endl;
}
//./tcpserver 8080 192.168.232.200
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        Debugprint(argv[0]);
        exit(0);
    }
    srand(time(nullptr));
    std::string serverip=argv[2];
    uint16_t serverport=std::stoi(argv[1]);

    Sock sockfd;
    bool r=sockfd.Socket();
    if(!r) return 1;
    sockfd.Connect(serverip,serverport);
    int cnt=1;
    const std::string opers="+-*/%=&^~";//操作码
    std::string inbuffer_stream;
    while(cnt<=10)
    {
        std::cout<<"=============第"<<cnt<<"次测试==============="<<std::endl;
        int x=rand()%100+1;
        usleep(123);
        int y=rand()%100;
        usleep(321);
        char op=opers[rand()%opers.size()];
        Request req(x,y,op);
        req.Debugprint();
        std::string package;
        req.Serialize(package);
        package=Encode(package);
        std::cout<<"最新请求:\n"<<package;
        write(sockfd.getfd(),package.c_str(),package.size());
        char buffer[128];
        ssize_t n=read(sockfd.getfd(),buffer,sizeof(buffer));
        if(n>0)
        {
            buffer[n]='\0';
            inbuffer_stream+=buffer;
            std::string content;
            bool r=Decode(inbuffer_stream,content);
            //assert(r);
            Response res;
            r=res.Deserialize(content);
            assert(r);
            res.Debugprint();
        }
        cnt++;
        sleep(1);
        std::cout<<"=============================="<<std::endl;
    }
    return 0;
}